package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.rare;

import net.minecraft.block.BlockState;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;
import net.xxcxcxcx.xc_enhancedmob.skills.SkillRarity;

import java.util.LinkedList;
import java.util.Queue;

public class HurricaneSkill extends BaseSkill {

    public Queue<DamageSource> damageSources = new LinkedList<>();

    public HurricaneSkill(int level) {
        super(level);
    }

    public HurricaneSkill(NbtCompound nbtCompound) {
        super(nbtCompound);
    }

    public HurricaneSkill() {
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return String.format("飓风技能，将击杀者随机传送。");
    }

    @Override
    public SkillRarity getSkillRarity() {
        return SkillRarity.Rare;
    }

    @Override
    public boolean onDeath(LivingEntity attacker, MobEntity target, DamageSource source, boolean ismob, boolean remote) {
        if (attacker == null) return true;
        for (int i = 0; i < 32; ++i) {
            teleportRandomly(attacker);
        }
        return true;
    }

    private void teleportRandomly(LivingEntity livingEntity) {
        if (!livingEntity.world.isClient() && livingEntity.isAlive()) {
            double d = livingEntity.getX() + (livingEntity.getRandom().nextDouble() - 0.5D) * 25.0D;
            double e = livingEntity.getY() + (double) (livingEntity.getRandom().nextInt(64) - 32);
            double f = livingEntity.getZ() + (livingEntity.getRandom().nextDouble() - 0.5D) * 25.0D;
            this.teleportTo(d, e, f, livingEntity);
        }
    }

    private void teleportTo(double x, double y, double z, LivingEntity livingEntity) {
        BlockPos.Mutable mutable = new BlockPos.Mutable(x, y, z);

        while (mutable.getY() > 0 && !livingEntity.world.getBlockState(mutable).getMaterial().blocksMovement()) {
            mutable.move(Direction.DOWN);
        }

        BlockState blockState = livingEntity.world.getBlockState(mutable);
        boolean bl = blockState.getMaterial().blocksMovement();
        if (bl) {
            boolean bl3 = livingEntity.teleport(x, y, z, false);
            if (bl3 && !livingEntity.isSilent()) {
                livingEntity.world.playSound(null, livingEntity.prevX, livingEntity.prevY, livingEntity.prevZ, SoundEvents.ENTITY_ENDERMAN_TELEPORT, livingEntity.getSoundCategory(), 1.0F, 1.0F);
                livingEntity.playSound(SoundEvents.ENTITY_ENDERMAN_TELEPORT, 1.0F, 1.0F);
            }
        }
    }
}
